package com.ciwong.epaper.modules.me.ui;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.TimeoutError;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.me.RefreshClass;
import com.ciwong.epaper.modules.me.bean.Clazz;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.eventbus.EventBus;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.net.URLEncoder;

/**
 * @description:修改班级名页面
 * @author: wyk
 * @time: 2021/5/25 9:29
 */
public class UpdateClassNameActivity extends BaseActivity {

    private TextView from_school_text;
    private EditText my_class_name;
    private TextView my_info_account;
    private TextView count_class_stu;
    private TextView count_vip;
    private RelativeLayout myinfo_out_lay;

    //vip人数
    private int vipCount;
    //年级名
    private String gradeName;
    //学校名
    private String schoolName;
    //班级名
    private String className;
    //修改后班级名
    private String classAliasName;
    //班级id
    private int classId;
    //年级id
    private int gradeId;
    //学生人数
    private int stuCount;
    //学段id
    private int periodId;


    @Override
    protected int setView() {
        return R.layout.activity_upe_class;
    }

    @Override
    protected void findViews() {
        from_school_text = findViewById(R.id.from_school_text);
        my_class_name = findViewById(R.id.my_class_name);
        my_info_account = findViewById(R.id.my_info_account);
        count_class_stu = findViewById(R.id.count_class_stu);
        count_vip = findViewById(R.id.count_vip);
        myinfo_out_lay = findViewById(R.id.myinfo_out_lay);
    }

    @Override
    protected void initEvent() {
        myinfo_out_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateClassNameById();
            }
        });
        my_class_name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    my_class_name.setHint("");
                } else {
                    my_class_name.setHint(className);
                }
            }
        });
    }

    @Override
    protected void init() {
        setTitleText("修改班级");
        Intent intent = getIntent();
        className = intent.getStringExtra(IntentFlag.INTENT_FLAG_CLASS_NAME);
        schoolName = intent.getStringExtra(IntentFlag.INTENT_FLAG_SCHOOL_NAME);
        stuCount = intent.getIntExtra(IntentFlag.INTENT_FLAG_STU_COUNT, 0);
        vipCount = intent.getIntExtra(IntentFlag.INTENT_FLAG_VIP_COUNT, 0);
        gradeId = intent.getIntExtra(IntentFlag.INTENT_FLAG_GRADE_ID, 0);
        classId = intent.getIntExtra(IntentFlag.INTENT_FLAG_CLASS_ID, 0);
        from_school_text.setText(schoolName);
        my_class_name.setText(className);
        my_class_name.setSelection(className.length());//将光标移至文字末尾
        count_class_stu.setText(stuCount + "");
        count_vip.setText(vipCount + "");
        setGrade();
        my_info_account.setText(gradeName);
        EApplication application = (EApplication) getBaseApplication();
        if (null != application) {
            Clazz clazz = application.getClazz();
            if (null != clazz) {
                periodId = clazz.getPeriod();
            }
        }

    }

    public static String toUtf8(String str) {
        String result = null;
        try {
            result = URLEncoder.encode(str,"UTF-8");
            result = result.replace("+", "%20");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 修改班级名
     */
    public void updateClassNameById() {
        if (null != my_class_name){
            classAliasName = my_class_name.getText().toString();
            classAliasName = toUtf8(classAliasName);
        }
        EpaperDao.getInstance().updateClassNameById(classId, periodId, gradeId, classAliasName, new BaseCallBack() {
            @Override
            public void failed(int errorCode, Object data) {
                //super.failed(errorCode,data);
                //ToastUtil.INSTANCE.toastCenterError("修改失败，请稍后再试");
            }

            @Override
            public void failed(Object data) {
                hideCricleProgress();
                if (data instanceof TimeoutError) {
                    ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                }
            }

            @Override
            public void success(Object data) {
                super.success(data);
                postClazz();
                if (null != data) {
                    Boolean isSuccess = (Boolean) data;
                    ToastUtil.INSTANCE.toastCenterSuccess("修改成功");
                    setUpeResult();
                }
            }
        });
    }

    public void postClazz(){
        EventBus.getDefault().post(new RefreshClass(true));
    }

    /**
     * 设置年级
     */
    public void setGrade() {
        switch (gradeId) {
            case 1101:
                gradeName = "一年级";
                break;
            case 1102:
                gradeName = "二年级";
                break;
            case 1103:
                gradeName = "三年级";
                break;
            case 1104:
                gradeName = "四年级";
                break;
            case 1105:
                gradeName = "五年级";
                break;
            case 1106:
                gradeName = "六年级";
                break;
            case 1107:
                gradeName = "七年级";
                break;
            case 1108:
                gradeName = "八年级";
                break;
            case 1109:
                gradeName = "九年级";
                break;
            case 1110:
                gradeName = "高一";
                break;
            case 1111:
                gradeName = "高一";
                break;
            case 1112:
                gradeName = "高二";
                break;
            case 1113:
                gradeName = "高三";
                break;
            case 1114:
                gradeName = "小班";
                break;
            case 1115:
                gradeName = "中班";
                break;
            case 1116:
                gradeName = "大班";
                break;
            case 1117:
                gradeName = "学前班";
                break;
            case 1118:
                gradeName = "大一";
                break;
            case 1119:
                gradeName = "大二";
                break;
            case 1120:
                gradeName = "大三";
                break;
            case 11121:
                gradeName = "大四";
                break;
            default:
                gradeName = "暂无";
                break;

        }
    }
   //设置修改结果 返回给上一个页面班级名 用于显示
    public void setUpeResult() {
        Intent intent = new Intent();
        String title = "";
        if (null != my_class_name){
             title = my_class_name.getText().toString();
        }
        intent.putExtra(IntentFlag.INTENT_FLAG_CLASS_NAME, title);
        setResult(RESULT_OK, intent);
    }

    @Override
    protected void loadData() {

    }
}
